/*
 * comm_mem.h
 *
 *  Created on: 2010-1-7
 *      Author: gavinkwoe
 */

#ifndef COMM_MEM_H_
#define COMM_MEM_H_

#include "comm_features.h"
#include "comm_std.h"
#include "comm_objbase.h"

//#include "cx_typedef.h"
//#include "cx_memory.h"
//#include "cx_string.h"

//#include "ctype.h"

/*************************************************************************
* INTERFACE
*************************************************************************/

COMM_INTERFACE_( ICommAllocator, 1, ICommO )
{
	__COMM_INHERIT_FROM( ICommO )

	COMM_METHOD( GetRemain )	( COMM_REF(ICommO) thiz, OUT unsigned long * psize );
	COMM_METHOD( Alloc )		( COMM_REF(ICommO) thiz, IN unsigned long size, OUT void ** ppmem, ... );
	COMM_METHOD( Free )		( COMM_REF(ICommO) thiz, IN void * pmem );
	COMM_METHOD( Copy )		( COMM_REF(ICommO) thiz, IN void * dst, IN void * src, IN unsigned long size );
};

/*************************************************************************
* DECLARATION
*************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif	// #if defined(__cplusplus)

COMM_IMPORT void				comm_heap_init( void );
COMM_IMPORT void				comm_heap_deinit( void );
#if defined(__COMM_DEBUG__)
COMM_IMPORT void *				comm_heap_alloc( unsigned long size, unsigned char * file, unsigned long line );
#else	// #if defined(__COMM_DEBUG__)
COMM_IMPORT void *				comm_heap_alloc( unsigned long size );
#endif	// #if defined(__COMM_DEBUG__)
COMM_IMPORT void				comm_heap_free( void * addr );
COMM_IMPORT void				comm_heap_dump( void );

COMM_IMPORT COMM_REF(ICommO)	comm_create_allocator( void * reserved );

COMM_IMPORT void				comm_memset( void * a, unsigned char b, unsigned long c );
COMM_IMPORT void				comm_memcpy( void * a, const void * b, unsigned long c );

#if defined(__cplusplus)
}
#endif	// #if defined(__cplusplus)

/*************************************************************************
* MACRO
*************************************************************************/

#if defined(__COMM_DEBUG__)
#define COMM_HEAP_ALLOC( __size )	comm_heap_alloc( __size, (unsigned char *)__FILE__, (unsigned long)__LINE__ )
#else	// #if defined(__COMM_DEBUG__)
#define COMM_HEAP_ALLOC( __size )	comm_heap_alloc( __size )
#endif	// #if defined(__COMM_DEBUG__)

#define COMM_HEAP_FREE( __p )	\
		if ( NULL != (__p) ) \
		{ \
			comm_heap_free( (void *)__p ); \
			(__p) = NULL; \
		}

#define COMM_MEMSET( a, b, c )				comm_memset( a, b, c )
#define COMM_MEMCPY( a, b, c )				comm_memcpy( a, b, c ) // cx_memcpy( (QVOID *)a, (CONST QVOID *)b, (QUINT)c )
#define COMM_MEMCMP( a, b, c )				memcpy( a, b, c ) // cx_memcpy( (QVOID *)a, (CONST QVOID *)b, (QUINT)c )

#define COMM_WCSCMP( a, b )				wcscmp( a, b ) // cx_strcmp_w( (CONST QWCHAR *)a, (CONST QWCHAR *)b )		
#define COMM_TOUPPER( c )					toupper( (int)c )

#define ICommAllocator_VTBL( p )			COMM_GET_VTBL_(p, ICommAllocator)
#define ICommAllocator_CALL( p, a )		ICommAllocator_VTBL(p)->a
#define ICommAllocator_GetRemain( p, a )	ICommAllocator_CALL( p, GetRemain )( p, a )

#if defined(__COMM_DEBUG__)
#define ICommAllocator_Alloc( p, a, b )	ICommAllocator_CALL( p, Alloc )( p, a, b, (unsigned char *)__FILE__, (unsigned long)__LINE__ )
#else	// #if defined(__COMM_DEBUG__)
#define ICommAllocator_Alloc( p, a, b )	ICommAllocator_CALL( p, Alloc )( p, a, b )
#endif	// #if defined(__COMM_DEBUG__)

#define ICommAllocator_Free( p, a )		ICommAllocator_CALL( p, Free )( p, a )
#define ICommAllocator_Copy( p, a, b, c )	ICommAllocator_CALL( p, Copy )( p, a, b, c )

#endif /* COMM_MEM_H_ */
